import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    ChangeDetectorRef,
    ElementRef,
    Output,
    EventEmitter
} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    Base_DataItemServiceProxy,
    Base_DataItemDetailEditDto,
    Base_DataItemDetailServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';

export interface IDataitemDetailUnitOnEdit {
    id?: number;
    f_ItemValue?: string;
    f_ItemName?: string;
    f_ItemCode?: string;
    f_ItemId?: number;
    parentId?: number;
}

@Component({
    selector: 'createOrEditDataItemDetailModal',
    templateUrl: './create-or-edit-dataitem-detail-modal.component.html',
    animations: [appModuleAnimation()]
})
export class CreateOrEditDataItemDetailModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _baseItemService: Base_DataItemServiceProxy,
        private _baseItemDetailService: Base_DataItemDetailServiceProxy,
        private _changeDetector: ChangeDetectorRef
    ) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dataitemUnitDisplayName') dataitemUnitDisplayNameInput: ElementRef;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    dataitemDetailUnit: IDataitemDetailUnitOnEdit = {};
    active = false;
    saving = false;
    partent: number;

    ngOnInit(): void {}
    onShown(): void {
        document.getElementById('dataitemUnitDisplayName').focus();
    }
    show(record): void {
        if (record == null) {
            this.notify.error(this.l('请选择字典分类'));
        } else {
            // if (record.id == null) {
            //     this.partent = record;
            // } else {
            //     this.dataitemDetailUnit.id = record.id;
            //     this.dataitemDetailUnit.itemId = record.f_ItemId;
            //     this.dataitemDetailUnit.code = record.f_ItemCode;
            //     this.dataitemDetailUnit.displayName = record.f_ItemName;
            //     this.dataitemDetailUnit.value = record.f_ItemValue;
            // }
            this.dataitemDetailUnit = record;
            this.active = true;
            this.modal.show();
            this._changeDetector.detectChanges();
        }
    }
    close(): void {
        this.modal.hide();
        this.active = false;
    }
    save(): void {
        const createInput = new Base_DataItemDetailEditDto();
        createInput.id = this.dataitemDetailUnit.id;
        createInput.f_ItemValue = this.dataitemDetailUnit.f_ItemValue;
        createInput.f_ItemName = this.dataitemDetailUnit.f_ItemName;
        createInput.f_ItemCode = this.dataitemDetailUnit.f_ItemCode;

        //   createInput.f_Remark = this.dataitemDetailUnit.remark;
        if (this.dataitemDetailUnit.f_ItemId == null) {
            this.dataitemDetailUnit.f_ItemId = this.dataitemDetailUnit.parentId;
        }
        createInput.f_ItemId = this.dataitemDetailUnit.f_ItemId;

        this.saving = true;
        this._baseItemDetailService
            .createOrUpdateDataItemDetail(createInput)
            .pipe(finalize(() => (this.saving = false)))
            .subscribe(result => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(result);
            });
    }
}
